package com.jie.educenter.controller;


import com.jie.commonutils.JwtUtils;
import com.jie.commonutils.ResultEntity;
import com.jie.commonutils.ordervo.UcenterMemberOrder;
import com.jie.educenter.entity.LoginMember;
import com.jie.educenter.entity.UcenterMember;
import com.jie.educenter.entity.vo.RegisterVO;
import com.jie.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author xzj
 * @since 2020-10-15
 */
//@CrossOrigin  // 解决跨域问题
@RestController
@RequestMapping("/educenter/member")
public class UcenterMemberController {

    private final UcenterMemberService memberService;

    @Autowired
    public UcenterMemberController(UcenterMemberService memberService) {
        this.memberService = memberService;
    }

    /**
     * 登录
     * @param member  传入的用户登录信息
     * @return        返回token信息
     */
    @PostMapping("/login")
    public ResultEntity loginUser(
            @RequestBody LoginMember member
            ) {
        // 调用service方法实现登录
        // 返回token值, 使用jwt生成
        String token = memberService.login(member);
        return ResultEntity.ok().data("token", token);
    }

    /**
     * 注册
     * @param registerVO  用户注册信息
     * @return            返回是否成功
     */
    @PostMapping("/register")
    public ResultEntity registerUser(
            @RequestBody RegisterVO registerVO
    ) {
        memberService.register(registerVO);
        return ResultEntity.ok();
    }

    /**
     * 根据token获取用户信息
     * @param request  获取请求信息, 根据请求信息得到token
     * @return         返回用户信息
     */
    @GetMapping("/getMemberInfo")
    public ResultEntity getMemberInfo(
            HttpServletRequest request
    ) {
        // 调用jwt工具类的方法, 根据request对象获取头信息, 返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        // 根据用户id查询数据库获取用户信息
        UcenterMember member = memberService.getById(memberId);
        return ResultEntity.ok().data("userInfo", member);
    }

    /**
     * 根据用户id获取用户信息
     * 用于被远程调用
     * @param id  用户id
     * @return    返回用户信息
     */
    @PostMapping("/getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(
            @PathVariable String id
    ) {
        UcenterMember member = memberService.getById(id);
        // 将member的值复制到UcenterMemberOrder对象中
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member, ucenterMemberOrder);
        return ucenterMemberOrder;
    }

    /**
     * 查询某一天注册人数
     * @param date  某一天
     * @return      返回某一天的注册人数
     */
    @GetMapping("/countRegister/{date}")
    public ResultEntity countRegister(
            @PathVariable("date") String date
    ) {
        Integer count = memberService.countRegisterDay(date);
        return ResultEntity.ok().data("countRegister", count);
    }

}

